package com.dckj.auto.common.interceptor;

import com.alibaba.fastjson.JSONArray;
import com.dckj.auto.controller.help.UserMenuHelp;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;

/**
 * jsp 标签权限拦截
 */
public class AuthorityJSP extends BodyTagSupport {

    private String target;//  acApplication/forMain.do
    @Override
    public int doStartTag() throws JspException { // 在标签开始处出发该方法
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        //获取用户拥有的权限
        if(StringUtils.isEmpty(target)){
            return BodyTagSupport.EVAL_BODY_INCLUDE;// 返回此则执行标签body中内容，SKIP_BODY则不执行
        }
        //判断是否有标签访问权限
        JSONArray jsonArray = UserMenuHelp.userAuthMenu(request,1);
        if(null == jsonArray){
            return BodyTagSupport.SKIP_BODY;
        }
        String auto = jsonArray.toString();
        if (!auto.contains(target)){
            return BodyTagSupport.SKIP_BODY;
        }
        return BodyTagSupport.EVAL_BODY_INCLUDE;// 返回此则执行标签body中内容，SKIP_BODY则不执行
    }

    @Override
    public int doEndTag() throws JspException {
        return BodyTagSupport.EVAL_BODY_INCLUDE;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }

}
